package boundary;
import control.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.Dimension;
import java.awt.Image;
import javax.swing.ImageIcon;
import entity.*;

/**
 * The Class BoardPanel.
 */
public class BoardPanel extends JPanel implements ActionListener {
    /**
	 * 
	 */
	private static final long serialVersionUID = 6L;
	private Timer timer;

	/** The img. */
    Image img;
	
	/** The _y. */
	public int _x=0, _y=0;
	private PeciesController peciesControl = new PeciesController();

	private Pecies moveToken = null;
	private Pecies moveObj = null;
	
	/** The count. */
	int count = 0; 
    
    /**
     * Instantiates a new board panel.
     */
    public BoardPanel() {
		this("/images/bg/bg.PNG");
        setFocusable(true);
        setBackground(Color.WHITE);
        setDoubleBuffered(true);
        moveToken = moveObj = (Pecies) GameMovableObj.getSpecificObj(0);
        timer = new Timer(25, this);
    }

	/**
	 * Instantiates a new board panel.
	 *
	 * @param img the img
	 */
	public BoardPanel(String img) {
		this.img = new ImageIcon(this.getClass().getResource(img)).getImage();
		Dimension size = new Dimension(400, 300);
		setPreferredSize(size);
		setMinimumSize(size);
		setMaximumSize(size);
		setSize(size);
		setLayout(null);
	}

	public void paintComponent(Graphics g) {
		g.setColor(Color.BLACK);
		g.fillRect(0,0,800,800);
		g.drawImage(img, _x, _y, null);
		int storedX=_x;
		int storedY=_y;
		
		if(moveToken!=moveObj) {

			shiftMoverObjToNextPlayer();
			for(int i=0; i<1;i++) {g.drawImage(img, storedX, storedY, null);}
			g.drawImage(img, _x, _y, null);
			timer.stop();
		}
		
		if(!timer.isRunning()) {
			drawSnapShot(storedX,storedY);
		}
		if(moveToken==moveObj && timer.isRunning()) {
			playerMove(storedX,storedY);
			moveObj.setNextImage();
		}
	}


    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D)g;
		
        ArrayList<Pecies> ms = peciesControl.getAllPecies();
         for (int i = 0; i < ms.size(); i++ ) {
			Pecies m =  ms.get(i);
			
			if(m!=moveObj)
				g2d.drawImage(m.getImage(), m.getX(), m.getY(), this);
			else 
				g2d.drawImage(m.getImage(),m.getX(), m.getY(), this);
         }
        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }


    public void actionPerformed(ActionEvent e) {
        repaint();  
    }

	/**
	 * Shift mover obj to next player.
	 */
	public void shiftMoverObjToNextPlayer() {

		moveObj.setCanvasSnapshot(_x,_y);
		moveObj.setCurrentPosition(moveObj.getX(),moveObj.getY());
		moveObj = (Pecies)moveToken;
		_x = moveObj.getCanvasX();
		_y = moveObj.getCanvasY();
		moveObj.setX(moveObj.getCurrentPositionX());
		moveObj.setY(moveObj.getCurrentPositionY());
		repaint();
	}

	/**
	 * Draw snap shot.
	 *
	 * @param storedX the stored x
	 * @param storedY the stored y
	 */
	public void drawSnapShot(int storedX, int storedY) {
		ArrayList<Pecies> al = peciesControl.getAllPecies();
		
		for(int i=0; i<al.size(); i++) {
			if(al.get(i) != moveObj	) {
				Pecies c = (Pecies)al.get(i);
				c.move(_x-storedX,_y-storedY);
			}
		}
	}
	
	/**
	 * Player move.
	 *
	 * @param storedX the stored x
	 * @param storedY the stored y
	 */
	public void playerMove(int storedX, int storedY) {
		peciesControl.playerMoveInBoard(this,moveObj);
		peciesControl.keepLocation(this, moveObj, storedX, storedY);
		
	}
	
	/**
	 * Shift token.
	 */
	public void shiftToken() {
		count++;
		moveToken = (Pecies) GameMovableObj.getGameMovableObj().get(count%GameMovableObj.getGameMovableObj().size());
	}
	/**
	 * Start move.
	 */
	public void startMove() {
		timer.start();
	}

	/**
	 * Stop move.
	 */
	public void stopMove() {
		timer.stop();
	}

	/**
	 * Gets the timer.
	 *
	 * @return the timer
	 */
	public Timer getTimer() {
		return timer;
	}
	
	/**
	 * Gets the move obj.
	 *
	 * @return the move obj
	 */
	public Pecies getMoveObj(){
		return moveObj;
	}
	
	/**
	 * Gets the move token obj.
	 *
	 * @return the move token obj
	 */
	public Pecies getMoveTokenObj() {
		return moveToken;
	}
}
